package com.fansl.koala.quickdev.module.users.controller;


import com.fansl.koala.quickdev.common.bean.PageBean;
import com.fansl.koala.quickdev.common.bean.R;
import com.fansl.koala.quickdev.module.users.bean.dto.SysUserDTO;
import com.fansl.koala.quickdev.module.users.entity.SysUser;
import com.fansl.koala.quickdev.module.users.service.ISysUserService;
import com.fansl.koala.quickdev.module.users.vo.SysUserVo;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fansl
 * @since 2020-03-13
 */
@RestController
@AllArgsConstructor
@RequestMapping("/users/sys-user")
public class SysUserController {
    private final ISysUserService sysUserService;

    /**
     * 添加
     *
     * @param sysUser 参数
     * @return TRUE/FALSE
     */
    @PostMapping("")
    public Mono<R<Boolean>> add(@RequestBody SysUser sysUser) {
        boolean b = sysUserService.save(sysUser);
        return Mono.just(R.ok(b));
    }

    /**
     * 根据主键更新
     *
     * @param param 更新参数
     * @return TRUE/FALSE
     */
    @PutMapping("")
    public Mono<R<Boolean>> updateById(@RequestBody SysUserDTO param) {
        boolean b = sysUserService.updateById(param);
        return Mono.just(R.ok(b));
    }

    /**
     * 根据主键获取信息
     *
     * @param id 主键
     * @return 查询数据
     */
    @GetMapping("/{id}")
    public Mono<R<SysUser>> getById(@PathVariable Long id) {
        SysUser sysUser = sysUserService.getById(id);
        return Mono.just(R.ok(sysUser));
    }

    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return TRUE/FALSE
     */
    @DeleteMapping("/{id}")
    public Mono<R<Boolean>> delById(@PathVariable Long id) {
        boolean b = sysUserService.removeById(id);
        return Mono.just(R.ok(b));
    }

    /**
     * 分页查询
     *
     * @param page    分页询参数
     * @param sysUser 查询参数
     * @return 查询数据
     */
    @GetMapping("/page")
    public Mono<R<PageBean<SysUserVo>>> page(PageBean<SysUserVo> page, SysUser sysUser) {
        //查询
        PageBean<SysUserVo> result = sysUserService.listPage(page, sysUser);
        return Mono.just(R.ok(result));
    }
}
